package com.zzh.takeoutapp.app;

import android.app.Service;
import android.content.Context;
import android.os.Vibrator;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.support.v7.app.AppCompatActivity;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.github.anzewei.parallaxbacklayout.ParallaxHelper;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cookie.store.MemoryCookieStore;
import com.lzy.okhttputils.cookie.store.PersistentCookieStore;
import com.zzh.takeoutapp.base.AppManager;
import com.zzh.takeoutapp.map.LocationService;

/**
 * Created by Administrator on 2018/7/11.
 */

public class MainApp extends MultiDexApplication {

    private static int TIME_MIN = 10000;
    private static MainApp instence;//單利模式
    private static String OK_HTTPUTILS = "OkHttpUtils";
    public Vibrator mVibrator;

    public static MainApp getInstence() {
        if (instence == null) {
            instence = new MainApp();
        }
        return instence;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(this);
        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        SDKInitializer.initialize(this);
        //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);
        mVibrator = (Vibrator) getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
        OkHttpUtils.init(this);
        OkHttpUtils.getInstance()//  //以下都不是必须的，根据需要自行选择
                .debug(OK_HTTPUTILS)//是否打开调试
                .setConnectTimeout(TIME_MIN)               //全局的连接超时时间
                .setReadTimeOut(TIME_MIN)                  //全局的读取超时时间OkHttpUtils.DEFAULT_MILLISECONDS
                .setWriteTimeOut(TIME_MIN)                //全局的写入超时时间
                .setCookieStore(new MemoryCookieStore())                           //cookie使用内存缓存（app退出后，cookie消失）
                .setCookieStore(new PersistentCookieStore());
        initPara();
    }

    public void initPara() {
        registerActivityLifecycleCallbacks(ParallaxHelper.getInstance());//侧滑初始化
    }

    public static Context getContext() {
        AppManager am = AppManager.getAppManager();
        if (am != null) {
            AppCompatActivity ba = am.currentActivity();
            return ba;
        } else {
            return null;
        }
    }
}
